package components;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;

public class Encrypter {
	/**
	 * Encipher a given string into a ciphertext, represented as an array of BigIntegers
	 * The ciphertext will be saved to ciphertext.txt and the plaintext will be saved to
	 * originalPlaintext.txt.
	 * 
	 * @param msg -	Message to encipher.
	 * @param pubKey - Public Key.
	 * @return cipherText - Array of enciphered characters.
	 */
	public BigInteger[] encipher(String msg, RSAKey pubKey){
		BigInteger[] cipherText = new BigInteger[msg.length()]; //Initialize ciphertext
		for(int i = 0; i < msg.length(); i++){ //iterate through chars in msg			
//			long currMs = now.getTimeInMillis();
			int ascii = msg.charAt(i); //ASCII value of char
//			System.out.println("ascii value: " + ascii);
			BigInteger intToBig = BigInteger.valueOf(ascii);//BigInteger representation of the ASCII int
			cipherText[i] = intToBig.modPow(pubKey.getExp(), pubKey.getN());
			System.out.println("Encrypted ASCII: " + cipherText[i]);
//			long currMs1 = now.getTimeInMillis();
//			System.out.println(currMs1-currMs + " milliseconds to encrypt message");
		}
		
		try {
			//create new plaintext file
			FileWriter origPt = new FileWriter("originalPlaintext.txt");
			BufferedWriter writer = new BufferedWriter(origPt);
			writer.write(msg);
			writer.close();

			StringBuilder toFile = new StringBuilder();
			for(BigInteger chunk : cipherText) {
			    toFile.append(chunk.intValue()).append(" ");
			}
//		    System.out.println(toFile.toString());
			
			//create new ciphertext file
			FileWriter output = new FileWriter("ciphertext.txt");
			BufferedWriter writer1 = new BufferedWriter(output);
//			System.out.println("toCipher: " + toFile.toString());
			writer1.write(toFile.toString());
			writer1.close();
			
		}catch(FileNotFoundException ex){
			System.out.println("Input file not found!");

		} catch (IOException e) {
			System.out.println("Cannot read from file!");
			e.printStackTrace();
		}

		return cipherText;
	}
	
	/**
	 * Encipher a given string into a ciphertext, represented as an array of BigIntegers
	 * The ciphertext will be saved to ciphertext.txt and the plaintext will be saved to
	 * originalPlaintext.txt. This method is aimed to allow the attacker to carry out 
	 * Weiner's attack.
	 * 
	 * @param msg -	Message to encipher.
	 * @param pubKey - Public Key.
	 * @return cipherText - Array of enciphered characters.
	 */
	public BigInteger[] weinerEncipher(String msg, RSAKey pubKey){
		BigInteger[] cipherText = new BigInteger[msg.length()]; //Initialize ciphertext
		for(int i = 0; i < msg.length(); i++){ //iterate through chars in msg			
//			long currMs = now.getTimeInMillis();
			int ascii = msg.charAt(i); //ASCII value of char
			System.out.println("ascii value: " + ascii);
			BigInteger intToBig = BigInteger.valueOf(ascii);//BigInteger representation of the ASCII int
			cipherText[i] = intToBig.modPow(pubKey.getN(), pubKey.getExp());
			System.out.println("Encrypted ASCII: " + cipherText[i]);
		}
		
		try {
			//create new plaintext file
			FileWriter origPt = new FileWriter("originalPlaintext.txt");
			BufferedWriter writer = new BufferedWriter(origPt);
			writer.write(msg);
			writer.close();

			StringBuilder toFile = new StringBuilder();
			for(BigInteger chunk : cipherText) {
			    toFile.append(chunk.intValue()).append(" ");
			}
//		    System.out.println(toFile.toString());
			
			//create new ciphertext file
			FileWriter output = new FileWriter("ciphertext.txt");
			BufferedWriter writer1 = new BufferedWriter(output);
//			System.out.println("toCipher: " + toFile.toString());
			writer1.write(toFile.toString());
			writer1.close();
			
		}catch(FileNotFoundException ex){
			System.out.println("Input file not found!");

		} catch (IOException e) {
			System.out.println("Cannot read from file!");
			e.printStackTrace();
		}

		return cipherText;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
